﻿using System.Windows;
using System.Collections.Specialized;
using System.Configuration;
using DrCarDrive.Util;
using System.Net;
using DrCarDrive.Wpf.WpfWindow;
using System.Windows.Input;
using System;
using System.IO;
using System.Drawing;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using DrCarDrive.Wpf.WpfPage;


namespace DrCarDrive.Wpf
{
    /// <summary>
    /// Interaction logic for DoctorHomePage.xaml
    /// </summary>
    public partial class DashboardPage : Page
    {
        private static DashboardPage instance;

        private DashboardPage()
        {
            InitializeComponent();
        }

        public static DashboardPage Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DashboardPage();
                }
                return instance;
            }
        }

        private void StartDriveButton_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.Instance.ContentFrame.Content = StartDrivePage.Instance;
        }

        private void AddVillageRecordButton_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.Instance.ContentFrame.Content =  AddVillageRecordPage.Instance;
        }

        private void FuelTopupRecordButton_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.Instance.ContentFrame.Content = FuelTopUp.Instance;
        }

        private void EndDriveButton_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.Instance.ContentFrame.Content = EndDrivePage.Instance;
        }


        private void setContinueState()
        {
            enableAllButtons();
            StartDriveButton.IsEnabled = false;
            lblCarNo.Content = SessionData.car.REG_NO;
            lblDriverName.Content = SessionData.drive.DRIVER_NAME;
            lblStartedOn.Content = SessionData.drive.START_DATE;
            lblFrom.Content = SessionData.drive.FROM;
            lblTo.Content = SessionData.drive.TO;
            if (SessionData.place != null)
            {
                lblLastVisit.Content = SessionData.place.VILLAGE_NAME;
                lblNumPatients.Content = SessionData.place.NUMBER_PATIENT;
                lblNumParamedics.Content = SessionData.place.NUMBER_PARAMEDICS;
                lblNumDoctors.Content = SessionData.place.NUMBER_DOCTORS;
                lblDistanceTravelled.Content = FindDifference.calculateDistance(SessionData.drive.START_ODOMETER,SessionData.place.ARRIVAL_ODOMETER);
                lblTimeTravelled.Text = FindDifference.calculateDuration(SessionData.drive.START_DATE, null);
            }
            gridCurrentStatus.Visibility = System.Windows.Visibility.Visible;
        }

        private void setNewState()
        {
            disableAllButtons();
            StartDriveButton.IsEnabled = true;
        }

        private void disableAllButtons()
        {
            StartDriveButton.IsEnabled = false;
            EndDriveButton.IsEnabled = false;
            AddVillageRecordButton.IsEnabled = false;
            FuelTopupRecordButton.IsEnabled = false;
            MedicalExpensesButton.IsEnabled = false;
            //MedicineRecordButton.IsEnabled = false;
        }

        private void enableAllButtons()
        {
            StartDriveButton.IsEnabled = true;
            EndDriveButton.IsEnabled = true;
            AddVillageRecordButton.IsEnabled = true;
            FuelTopupRecordButton.IsEnabled = true;
            MedicalExpensesButton.IsEnabled = true;
            //MedicineRecordButton.IsEnabled = true;
        }

        private void Page_Loaded_1(object sender, RoutedEventArgs e)
        {
            clearStatusLabels();
            gridCurrentStatus.Visibility = System.Windows.Visibility.Hidden;
            //MessageBox.Show("Test");
            if (SessionData.drive != null && SessionData.drive.STATUS == true)
            {
                setContinueState();
            }
            else
            {
                setNewState();
            }
        }

        private void clearStatusLabels()
        {
            lblCarNo.Content = "";
            lblDriverName.Content = "";
            lblLastVisit.Content = "";
            lblStartedOn.Content = "";
            lblFrom.Content = "";
            lblTo.Content = "";
            lblDistanceTravelled.Content = "";
            lblTimeTravelled.Text = "";
            lblNumDoctors.Content = "";
            lblNumParamedics.Content = "";
            lblNumPatients.Content = "";
        }

        private void MedicalExpensesButton_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.Instance.ContentFrame.Content = MedicalExpenses.Instance;
        }

        private void MedicineRecordButton_Click(object sender, RoutedEventArgs e)
        {
            //if (SessionData.place != null)
            //{
            //    MainWindow.Instance.ContentFrame.Content = IssueMedicine.Instance;
            //}
            //else
            //{
            //    MSGWindow errorWindow = new MSGWindow(Messages.TTL_OPEN_MEDICINE_PAGE, Messages.MSG_OPEN_MEDICINE_PAGE);
            //    errorWindow.Show();
            //}
            MainWindow.Instance.ContentFrame.Content = RecordHistoryPage.Instance;
        }        

    }
}
